package com.power.sifa.correction.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.id.NanoId;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.correction.model.Crc_geopoint;
import com.power.sifa.correction.model.Crw_rule_group;
import com.power.sifa.correction.service.Crc_geopointService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.power.sifa.correction.model.Crw_rule_geopoint;
import com.power.sifa.correction.dao.Crw_rule_geopointDao;
import com.power.sifa.correction.service.Crw_rule_geopointService;
import com.power.sifa.correction.model.pageInfo.Crw_rule_geopointPageInfo;

import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  规则相关范围区域业务层实现
 */
@Service
public class Crw_rule_geopointServiceImpl extends BaseServiceImpl implements Crw_rule_geopointService {

    @Autowired
    private Crw_rule_geopointDao dao;

    @Resource
    private Crc_geopointService crc_geopointService;

    @Override
    public Crw_rule_geopoint getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Crw_rule_geopoint crw_rule_geopoint) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (crw_rule_geopoint.getPrimaryKey() != null) {
        	 Crw_rule_geopoint crw_rule_geopointfDb = dao.findById(crw_rule_geopoint.getPrimaryKey()).orElse(null);
             if(crw_rule_geopointfDb != null) {
                BeanUtils.copyProperties(crw_rule_geopoint, crw_rule_geopointfDb);
                dao.save(crw_rule_geopointfDb);
                return;
             }
        }
        dao.save(crw_rule_geopoint);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Crw_rule_geopoint> findAll(Crw_rule_geopointPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<String> ids) {
        dao.removeByIds(ids);
    }
    
    @Override
	public List<Crw_rule_geopoint> findAll() {
		return dao.findAll();
	}
	
	@Override
	public void saveAll(List<Crw_rule_geopoint> list) {
		dao.saveAll(list);
	}

    @Override
    public List<Crc_geopoint> findGeopointsByRuleId(String ruleId) {
        List<Crw_rule_geopoint> geoPoints = dao.getByRuleId(ruleId);
        if (CollectionUtil.isNotEmpty(geoPoints)) {
            List<String> geoids = geoPoints.stream().map(Crw_rule_geopoint::getGeoid).collect(Collectors.toList());
            return crc_geopointService.getByIds(geoids);
        }
        return null;
    }

    @Override
    public void saveAll(String ruleId, List<String> geopointIds) {
        if (!CollectionUtils.isEmpty(geopointIds)) {
            List<Crw_rule_geopoint> geopoints =  geopointIds.stream().map(geopointId -> {
                Crw_rule_geopoint geopoint = new Crw_rule_geopoint();
                geopoint.setId(NanoId.randomNanoId());
                geopoint.setGeoid(geopointId);
                geopoint.setRuleid(ruleId);
                return geopoint;
            }).collect(Collectors.toList());
            this.saveAll(geopoints);
        }
    }

    @Override
    public void removeByRuleId(String ruleId) {
        dao.removeByRuleId(ruleId);
    }
}
